package com.wholeintech.dao;

import java.util.List;

import com.wholeintech.entity.KnxDevice;
import com.wholeintech.utils.DatabaseUtils;

import android.content.Context;

import org.xutils.DbManager;
import org.xutils.ex.DbException;

/**
 * @author Administrator
 */
public class KnxDeviceDao {
    private final static String TAG = "KnxDeviceDao";
    private Context mContext = null;

    public KnxDeviceDao(Context context) {
        this.mContext = context;
    }

    /**
     * 新增
     *
     * @param
     * @return
     */
    public boolean add(KnxDevice entity) {
        boolean bRet = false;
        DbManager db = DatabaseUtils.DeviceDbUtils();
        try {
            db.save(entity);
            bRet = true;
        } catch (DbException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                //db.close();
                //db = null;
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

        return bRet;
    }

    public boolean update(KnxDevice entity) {
        boolean bRet = false;
        DbManager db = DatabaseUtils.DeviceDbUtils();

        try {
            db.saveOrUpdate(entity);
            bRet = true;
        } catch (DbException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                //db.close();
                //db = null;
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

        return bRet;
    }

    /**
     * 删除
     *
     * @param intId
     */
    public void delete(int intId) {
        DbManager db = DatabaseUtils.DeviceDbUtils();

        try {
            db.deleteById(KnxDevice.class, intId);
        } catch (DbException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                //db.close();
                //db = null;
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    /**
     *
     */
    public void deleteAll() {
        DbManager db = DatabaseUtils.DeviceDbUtils();

        try {
            db.delete(KnxDevice.class);
        } catch (DbException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                //db.close();
                //db = null;
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    /**
     * @return
     */
    public List<KnxDevice> getKnxDeviceList() {
        DbManager db = DatabaseUtils.DeviceDbUtils();
        List<KnxDevice> list = null;

        try {
            list = db.findAll(KnxDevice.class);
            if (list != null) {
                list.size();
            }

        } catch (DbException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                //db.close();
                //db = null;
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

        return list;
    }

    /**
     * @param
     * @return
     */
    public KnxDevice getKnxDeviceById(int intId) {
        DbManager db = DatabaseUtils.DeviceDbUtils();
        KnxDevice knxDevice = null;

        try {
            knxDevice = db.selector(KnxDevice.class).where("id", "=", intId).findFirst();
        } catch (DbException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                //db.close();
                //db = null;
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

        return knxDevice;
    }

    /**
     * @param
     * @return
     */
    public KnxDevice getKnxDeviceByDevId(String strDevId) {
        DbManager db = DatabaseUtils.DeviceDbUtils();
        KnxDevice knxDevice = null;

        try {
            knxDevice = db.selector(KnxDevice.class).where("devId", "=", strDevId).findFirst();
        } catch (DbException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                //db.close();
                //db = null;
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

        return knxDevice;
    }
}
